//
//  WmcDatePickerView.swift
//  YPWatermarkCamera
//
//  Created by 周诗松 on 2022/5/28.
//

import Foundation
import YPBedrock

class WmcDatePickerView: WmcBaseView {
    
    var dateStr: String = "" {
        didSet {
            if let date = dateStr.yp.date(format: .iso8601Day, identifier: .asia(.Shanghai)) {
                datePickerView.date = date
            }
        }
    }
    
    lazy var startDate: Date? = {
        return "2020.01.01".yp.date(format: .iso8601Day, identifier: .asia(.Shanghai))
    }()
    
    var sureClosure: ((String) -> Void)?
    
    private lazy var maskLayer: CAShapeLayer = {
        let maskLayer = CAShapeLayer()
        return maskLayer
    }()
    
    private lazy var cancelBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("取消", for: .normal)
        btn.setTitleColor(.wmc_mainTheme, for: .normal)
        btn.titleLabel?.font = 16.wmc_font
        return btn
    }()
    
    private lazy var sureBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("确定", for: .normal)
        btn.setTitleColor(.wmc_mainTheme, for: .normal)
        btn.titleLabel?.font = 16.wmc_font
        return btn
    }()
    
    private lazy var titleLabel: UILabel = {
        let label = UILabel.YP.Create(font: 16.yp.uiFont(weight: .medium),
                                      color: UIColor.wmc_grayH2_323233,
                                      alignment: .center)
        label.text = "请选择日期"
        return label
    }()
    
    private lazy var datePickerView: UIDatePicker = {
        let datePicker = UIDatePicker()
        if #available(iOS 13.4, *) {
            datePicker.preferredDatePickerStyle = .wheels
        }
        datePicker.locale = Locale(identifier: "zh_CN")
        datePicker.datePickerMode = .date
        datePicker.timeZone = TimeZone(identifier:"Asia/Shanghai")
        datePicker.minimumDate = startDate
        datePicker.maximumDate = WmcDateManager.shared.networkDate.value ?? Date()
        datePicker.date = WmcDateManager.shared.networkDate.value ?? Date()
        datePicker.addTarget(
            self,
            action: #selector(dateDidChanged(datePicker:)),
            for: .valueChanged)
        return datePicker
    }()

    override func layoutSubviews() {
        super.layoutSubviews()
        let maskPath = UIBezierPath(roundedRect: bounds,
                                    byRoundingCorners: [.topLeft, .topRight],
                                    cornerRadii: 8.yp.cgSize)
        maskLayer.path = maskPath.cgPath
        layer.mask = maskLayer
    }

    override func makeUI() {
        super.makeUI()
        
        addSubview(titleLabel)
        addSubview(cancelBtn)
        addSubview(sureBtn)
        addSubview(datePickerView)
        
        titleLabel.snp.makeConstraints { make in
            make.centerY.equalTo(cancelBtn)
            make.centerX.equalToSuperview()
        }
        
        cancelBtn.snp.makeConstraints { make in
            make.top.equalTo(12)
            make.left.equalTo(10)
            make.width.equalTo(60)
            make.height.equalTo(25)
        }
        
        sureBtn.snp.makeConstraints { make in
            make.top.equalTo(12)
            make.right.equalTo(-10)
            make.width.equalTo(60)
            make.height.equalTo(25)
        }
        
        datePickerView.snp.makeConstraints { make in
            make.top.equalTo(sureBtn.snp.bottom).offset(15)
            make.left.right.equalToSuperview()
            make.bottom.equalTo(-Wmc_safeAreaBottom)
        }
    }
    
    override func bindViewAction() {
        cancelBtn.yp.addEvent { _ in
            YPApp.CurrentVC?.dismiss(animated: true)
        }
        
        sureBtn.yp.addEvent { [weak self] _ in
            guard let weakSelf = self else { return }
            weakSelf.sureClosure?(weakSelf.dateStr)
            YPApp.CurrentVC?.dismiss(animated: true)
        }
    }
    
    @objc
    private func dateDidChanged(datePicker: UIDatePicker) {
        let formatter = DateFormatter()
        formatter.timeZone = TimeZone(identifier:"Asia/Shanghai")
        formatter.dateFormat = "yyyy.MM.dd"
        dateStr = formatter.string(from: datePicker.date)
    }
}
